var NS = NS || {};
NS.App.Background = new Class({
	Extends: NS.App,

	/**
	 * Canvas context
	 * @var {Object}
	 */
	ctx: null,

	/**
	 * Finish flag
	 * @var {Boolean}
	 */
	isFinished: false,

	/**
	 * First check flag
	 * @var {Boolean}
	 */
	isFirstCheck: true,

	/**
	 * Notification
	 * @var {NS.Notification}
	 */
	notification: null,

	/**
	 * Constructor
	 */
	initialize: function(){
		this.parent();

		this.ctx = new Element('canvas', {id:'canvas', width:20, height:20})
			.inject(document.body)
			.getContext('2d');
		this.notification = new NS.Notification('finish', localStorage.fw_mp3_finish);
		this.check.periodical(1000, this);
		this.check();
	},

	/**
	 * Check
	 */
	check: function(){
		var arrStart = localStorage.fw_start.split(':'),
			arrEnd = localStorage.fw_end.split(':');

		var tsStart = this.getTimestamp(arrStart),
			tsEnd = this.getTimestamp(arrEnd);

		if (tsEnd < tsStart)
			tsEnd = this.getTimestamp(arrEnd, true);

		var status = (+new Date() - tsStart) / (tsEnd - tsStart);

		if (status > 1)
			status = 0;

		var sofar = 0,
			data = [status, 1-status],
			center = [10, 10],
			radius = 8,
			colors = ['rgb(85,99,127)', 'rgb(187,193,204)'],
			ctx = this.ctx;

		ctx.clearRect(0, 0, 19, 19);
		for (var piece in data) {
			ctx.beginPath();
			ctx.moveTo(center[0], center[1]);
			ctx.arc(
				center[0], center[1],
				radius,
				Math.PI * (- 0.5 + 2 * sofar),
				Math.PI * (- 0.5 + 2 * (sofar + data[piece])),
				false
			);
			ctx.lineTo(center[0], center[1]);
			ctx.closePath();
			ctx.fillStyle = colors[piece];
			ctx.fill();

			sofar += data[piece];
		}

		var ts = tsEnd - new Date(),
			hours = Math.floor(ts/36e5),
			minutes = Math.ceil((ts - hours*36e5)/6e4),
			seconds = Math.floor(Math.round((ts - hours*36e5)/1e3) - minutes*60);

		if (minutes == 60){
			minutes = 0;
			hours++;
		}

		var briefTitle = hours + ':' + (minutes > 9 ? minutes : '0' + minutes);

		chrome.browserAction.setIcon({imageData: this.ctx.getImageData(0, 0, 19, 19)});

		if (ts >= 0) {
			chrome.browserAction.setTitle({title: briefTitle});
			var badge = localStorage.fw_badge, badgeText = '';
			if (badge != 'none'){
				if (badge == 'brief' && briefTitle.length == 5)
					badge = 'hours';
				switch (badge) {
					case 'hours':
						badgeText = Math.round(ts/36e5) + ' h.';
						break;
					case 'brief':
						badgeText = briefTitle;
						break;
					case 'minutes':
						badgeText = Math.round(ts/6e4);
				}
			}
			chrome.browserAction.setBadgeText({text: badgeText + ''});

			if (hours == 0 && minutes == 0 && seconds < 10)
				chrome.browserAction.setBadgeText({text: seconds+''});
		}
		else {
			if (!this.isFinished)
				this.fin();
		}

		// Finish status
		this.isFinished = ts < 0;

		// First check status
		this.isFirstCheck = false;
	},

	/**
	 * Retrieves timestamp
	 * @param {Array} time
	 * @param {Boolean} incrementDay
	 * @return {Number}
	 */
	getTimestamp: function(time, incrementDay){
		var d = new Date();
		d.setHours(time[0]);
		d.setMinutes(time[1]);
		d.setSeconds(0);
		d.setMilliseconds(0);
		if (incrementDay)
			d.setDate(d.getDate() + 1);
		return +d;
	},

	/**
	 * Sets fin status
	 */
	fin: function(){
		chrome.browserAction.setTitle({title: 'Holy workend!'});
		chrome.browserAction.setBadgeText({text: 'Fin'});

		// Sound notification
		if (!this.isFirstCheck && !!+localStorage.fw_notify)
			this.notification.notify();
	}
});
